<template>
  <el-form :model="ruleForm" ref="ruleForm" :rules="rules"
           :class="isEdit?'':'formItem'"
           :label-position="labelPosition">
    <el-card class="box-card" shadow="never">
      <div slot="header" class="header-card">
        <span class="header-card-title">保养信息</span>
      </div>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="是否首保:" prop="maintainType">
            <el-select v-if="isEdit" style="width: 100%" v-model="ruleForm.maintainType" placeholder="是否首保">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="2"></el-option>
            </el-select>
            <span v-else>
              {{ ruleForm.maintainType == 1 ? '是' : ruleForm.maintainType == 2 ? '否' : '' }}
            </span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="8" v-if="isShow">
          <el-form-item label="预计保养开始时间:" prop="expectStartTime">
            <el-date-picker type="date" v-model="ruleForm.expectStartTime" v-if="isEdit" value-format='yyyy-MM-dd'
                            style="width: 100%;" placeholder="选择预计保养开始时间">
            </el-date-picker>
            <span v-else>{{ ruleForm.expectStartTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isShow">
          <el-form-item label="预计保养结束时间:" prop="expectEndTime">
            <el-date-picker type="date" v-model="ruleForm.expectEndTime" v-if="isEdit" value-format='yyyy-MM-dd'
                            style="width: 100%;" placeholder="选择预计保养结束时间">
            </el-date-picker>
            <span v-else>{{ ruleForm.expectEndTime }}</span>

          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isShow">
          <el-form-item label="签约保养厂/保养厂:" prop="maintainShopId">
            <el-select v-if="isEdit" v-model="ruleForm.maintainShopId"
                       filterable
                       @change="handleChange" style="width: 100%"
                       placeholder="请选择签约保养厂/保养厂">
              <el-option v-for="item in options" :key="item.supplierId" :label="item.supplierName"
                         :value="item.supplierId"/>
            </el-select>
            <span v-else>{{ ruleForm.maintainShop }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="8" v-if="isShow">
          <el-form-item label="故障描述:" prop="faultDescribe">
            <el-input v-if="isEdit" v-model="ruleForm.faultDescribe" placeholder="输入故障描述"></el-input>
            <span v-else>{{ ruleForm.faultDescribe }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isShow">
          <el-form-item label="保养厂报价:" prop="maintainQuoter">
            <el-input v-if="isEdit" disabled v-model=" ruleForm.maintainQuoter"
                      placeholder="输入保养厂报价"  v-thousand></el-input>
            <span v-else>{{ format_thousand(ruleForm.maintainQuoter) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isShow">
          <el-form-item label="是否符合索赔理由:">
            <el-select v-model="ruleForm.reasonForClaim" placeholder="选择是否符合索赔理由" style="width: 100%;"
                       v-if="isEdit">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="2"></el-option>
            </el-select>
            <span v-else>{{
                ruleForm.reasonForClaim == "1" ? '是' : ruleForm.reasonForClaim == "2" ? '否' : ''
              }}</span>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="8" v-if="isShow">
          <el-form-item label="是否有同车型(处置车辆)对换配件:">
            <el-select v-model="ruleForm.swappingAccessories" placeholder="选择是否有同车型(处置车辆)对换配件"
                       style="width: 100%;"
                       v-if="isEdit">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="2"></el-option>
            </el-select>
            <span v-else>{{
                ruleForm.swappingAccessories === "1" ? '是' : ruleForm.swappingAccessories ===
                "2" ? '否' : ''
              }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isShow && ruleForm.reasonForClaim == '2'">
          <el-form-item label="不符合索赔理由:">
            <el-input v-if="isEdit" v-model="ruleForm.claimantReason" placeholder="输入不符合索赔理由"></el-input>
            <span v-else>{{ ruleForm.claimantReason }}</span>
          </el-form-item>
        </el-col>

      </el-row>

      <el-row :gutter="24">
        <el-col v-if="isShow" :span="24">
          <el-form-item label="综合建议方案:" prop="comprehensiveRecommendations">
            <el-input v-if="isEdit" placeholder="输入综合建议方案" type="textarea" :rows="4"
                      v-model="ruleForm.comprehensiveRecommendations"></el-input>
            <span v-else>{{ ruleForm.comprehensiveRecommendations }}</span>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="24">
        <el-col v-if="isShow" :span="24">
          <el-form-item label="备注:">
            <el-input v-if="isEdit" placeholder="输入备注" type="textarea" :rows="4"
                      v-model="ruleForm.maintainRemark"></el-input>
            <span v-else>{{ ruleForm.maintainRemark }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-card>
  </el-form>
</template>

<script>
import UploadFile from "../../../../components/UploadFile";
import {effectDate} from "@/utils/libs/effectDate.js"
import {getInsuranceSelectList} from "@/api/cwgd/insuranceSelect";

export default {
  name: "upkeepBasceInfo",
  components: {UploadFile},


  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: '',
    }
  },
  computed: {
    ruleForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.isEdit) {
        return "top";
      } else {
        return "left";
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool
    },
  },
  watch: {
    'value.carMaintainProjectList': {
      handler(val) {
        if (val) {
          this.handleCount()
        }
      },
      immediate: true,
      deep: true
    }
  },
  data() {
    var validateMaintainProjectUrl = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请上传附件"));
      } else {
        callback();
      }
    };
    var start = (rule, value, callback) => {
      let that = this
      effectDate(rule, value, callback,
          that.ruleForm.expectStartTime,
          that.ruleForm.expectEndTime,
          '预计保养开始时间',
          '预计保养结束时间'
      )
    };
    var end = (rule, value, callback) => {
      let that = this
      effectDate(rule, value, callback,
          that.ruleForm.expectStartTime,
          that.ruleForm.expectEndTime,
          '预计保养开始时间',
          '预计保养结束时间'
      )
    };
    return {
      isShow: true,
      addAmountNun: 0,
      rules: {
        expectStartTime: [
          {
            required: this.state === 'write',
            validator: start,
            trigger: "change",
          },
        ],
        expectEndTime: [
          {
            required: this.state === 'write',
            validator: end,
            trigger: "change",
          },
        ],
        maintainShopId: [
          {
            required: this.state === 'write',
            message: "请选择签约保养厂",
            trigger: "change",
          },
        ],
        faultDescribe: [
          {required: this.state === 'write', message: "请输入故障描述", trigger: "blur"},
        ],
        maintainQuoter: [
          {
            required: this.state === 'write',
            message: "请输入保养厂报价",
            trigger: "blur",
          },
        ],
        comprehensiveRecommendations: [
          {
            required: this.state === 'write',
            message: "请输入综合建议方案",
            trigger: "blur",
          },
        ],
        maintainProjectUrl: [
          {
            required: this.state === 'write',
            validator: validateMaintainProjectUrl,
            trigger: "change",
          },
        ],
      },
      options: []
    };
  },
  created() {
    this.handleList()

  },
  mounted() {

  },
  methods: {
    handleList() {
      getInsuranceSelectList({businessTypeCode: "4"}).then((res) => {
        if (res.code === 200) {
          this.options = res.data;
        }
      });
    },

    handleChange(val) {
      this.value.maintainShopId = this.ruleForm.maintainShopId = val;
      this.value.maintainShop = this.ruleForm.maintainShop = this.options.find(
          (item) => item.supplierId === val
      ).supplierName;
    },

    handleCount() {
      if (Number(this.value.maintainType) === 1) {
        this.value.maintainQuoter = 0
      } else {
        let num = 0
        for (let i = 0; i < this.value.carMaintainProjectList.length; i++) {
          num += Number(this.value.carMaintainProjectList[i].price)
        }
        this.$set(this.value, 'maintainQuoter', num)
      }
    },


    //提交效验
    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            resolve();
          } else {
            this.locateToErr()
            reject();
          }
        });
      });
    },

    locateToErr() {

      this.$nextTick(() => {
        let isError = document.getElementsByClassName('is-error')
        isError[0].scrollIntoView({
          // 滚动到指定节点
          // 值有start,center,end，nearest，当前显示在视图区域中间
          block: 'center',
          // 值有auto、instant,smooth，缓动动画（当前是慢速的）
          behavior: 'smooth',
        })
      })
    },

    valid() {
      return new Promise((resolve) => {
        this.$refs.ruleForm.validate((valid) => {
          resolve(valid);
        });
      });
    },

  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";
</style>


